package com.yuanli.cosmeticmirror.view.adapter;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.cgfay.filter.glfilter.resource.bean.ResourceData;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.event.MainEvent;
import com.yuanli.cosmeticmirror.utils.BitmapUtils;
import com.yuanli.cosmeticmirror.widget.FilterImageView;

import org.greenrobot.eventbus.EventBus;

import comenjoy.com.imageloadlibrary.GlideUtil;

import static com.yuanli.cosmeticmirror.base.Constant.ACTION_START_FILTER_SWITCHING;

/**
 * 滤镜适配器
 */
public class FilterAdapter extends BaseQuickAdapter<ResourceData, BaseViewHolder> {
    public int position = MyApp.POSITION;

    public FilterAdapter() {
        super(R.layout.items_filter);
    }


    @Override
    protected void convert(BaseViewHolder helper, ResourceData item) {
        FilterImageView imageView = helper.getView(R.id.filter_img);
        if (item.thumbPath.startsWith("assets://")) {
            GlideUtil.getInstance().loadImage(MyApp.getInstance(), BitmapUtils.getImageFromAssetsFile(MyApp.getInstance(), item.thumbPath.substring("assets://".length())), imageView);
        } else {
            GlideUtil.getInstance().loadImage(MyApp.getInstance(), BitmapUtils.getBitmapFromFile(item.thumbPath), imageView);
        }
        helper.setText(R.id.filter_name, item.name);
        item.isCheck = position == helper.getAdapterPosition();
        imageView.setCheck(position == helper.getAdapterPosition());
        if (helper.getAdapterPosition() > 11) {
            imageView.setZheZhao(!MyApp.IsVIP);
        } else {
            imageView.setZheZhao(false);
        }

    }

    public void setPosition(int position) {
        if (position == this.position) {
            this.position = -1;
            MyApp.POSITION = -1;
        } else {
            this.position = position;
        }
        notifyItemRangeChanged(0, getItemCount());
    }
}
